<?php

namespace App\Http\Controllers\Admin;

use App\Models\Contract;
use App\Models\ContractDetail;
use App\Models\Houses;
use App\Models\Project;
use App\Models\Survey;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ContractController extends Controller
{
    // 合同列表
    public function index(Request $request)
    {

        $projects = Project::all();
        $blocks = Survey::select('id', 'block_name')->get();
        $houses = Houses::all();

        $where = function ($query) use ($request) {
            if ($request->has('is_sign') and $request->is_sign != '') {
                $query->whereIsSign($request->is_sign);
            }

            if ($request->has('project_id') and $request->project_id != '') {
                $query->whereProjectId($request->project_id);
            }

            if ($request->has('block_id') and $request->block_id != '') {
                $query->whereBlockId($request->block_id);
            }


        };


        $where_house = function ($query) use ($request) {
            if ($request->has('levy_house_num') and $request->levy_house_num != '') {
                $query->where('levy_house_num' ,$request->levy_house_num);
            }


            if ($request->has('owner') and $request->owner != '') {
                $query->where('owner', $request->owner);
            }
        };

        $contracts = Contract::with(['projects' => function ($query) {
            $query->where('status', 2);

        }])->with(['houses' => function($query) use($where_house){

            $query->where($where_house);

        }])->where($where)->paginate(10);


        return view('admin.contract.index', compact('projects', 'blocks', 'houses', 'contracts'));
    }

    // 是否签订合同
    public function change_on(Request $request)
    {
        $id = $request->id;

        $contract = Contract::find($id);

        $value = $contract->is_sign ? false : true;
        $contract->is_sign = $value;

        $contract->save();

        if ($value == 1) {
            return ['msg' => '合同签订成功~~', 'status' => 1];
        } else {
            return ['msg' => '合同签订失败~~', 'status' => 1];
        }

    }

    // 是否发送审计
    public function is_send(Request $request)
    {
        $id = $request->id;

        $contract = Contract::find($id);
        if ($contract['contract_img'] == '') {
            return ['msg' => '请先上传合同图片', 'status' => 0];
        }

        if ($contract['is_sign'] == 0) {
            return ['msg' => '请先签订合同！', 'status' => 0];
        }


        $value = $contract->is_send_audit ? false : true;
        $contract->is_send_audit = $value;

        $contract->save();

        if ($value == 1) {
            return ['msg' => '发送审计成功~~', 'status' => 1];
        } else {
            return ['msg' => '发送审计失败~~', 'status' => 1];
        }

    }

    // 查看合同图片
    public function show_img(Request $request)
    {
        $id = $request->id;

        $contract = Contract::select('id', 'contract_img')->find($id);
        $contract['src'] =$contract['contract_img'];

        return response()->json(['data' => [$contract] ]);

    }

    // 申请支付
    public function is_send_pay(Request $request)
    {
        $id = $request->id;

        $contract = Contract::find($id);

        if ($contract['is_sign'] == 0) {
            return ['msg' => '请先签订合同！', 'status' => 0];
        }

        if ($contract['is_audit'] == 0) {
            return ['msg' => '请先进行审计通过！', 'status' => 0];
        }

        $value = $contract->is_send_pay ? false : true;
        $contract->is_send_pay = $value;

        $contract->save();

        if ($value == 1) {
            return ['msg' => '申请支付成功~~', 'status' => 1];
        } else {
            return ['msg' => '申请支付失败~~', 'status' => 1];
        }
    }

    // 上传合同图片
    public function upload_img(Request $request)
    {
        $id = $request->id;

        $contract = Contract::find($id);

        $contract->contract_img = $request->contract_img;
        $contract->save();

        return ['msg' => '上传成功~~', 'status' => 1];
    }


    // 编辑合同
    public function editContract(Request $request)
    {
        $arr=explode('=',$request->contractId);
        $contractId=$arr['1'];
        $contractInfo= Contract::with('houses')->with('details')->find($contractId);


        return view('admin.contract.editContract',compact('contractInfo'));
    }

    // 更新合同
    public function updateContract(Request $request)
    {
        $data = [
            'repay_type' => $request->repay_type,
            'repay_house_address' => $request->repay_house_address,
            'repay_built_area' => $request->repay_built_area,
            'repay_money' => $request->repay_money,
        ];

        Contract::where('id', $request->id)->update($data);

        $contract_detail = [
            'contract_id' => $request->id,
            'house_money' => $request->house_money,
            'unregistered_money' => $request->unregistered_money,
            'decorate_money' => $request->decorate_money,
            'resettlement_money' => $request->resettlement_money,
            'removal_money' => $request->removal_money,
            'house_facilities_money' => $request->house_facilities_money,
            'water_meter_money' => $request->water_meter_money,
            'electric_mete_money' => $request->electric_mete_money,
            'cable_tv_money' => $request->cable_tv_money,
            'air_conditioning_money' => $request->air_conditioning_money,
            'solar_water_heater' => $request->solar_water_heater,
            'electric_water_heater' => $request->electric_water_heater,
            'structures_money' => $request->structures_money,
            'built_area_money' => $request->built_area_money,
            'equity_replace_money' => $request->equity_replace_money,
            'life_money' => $request->life_money,
            'remova_reward' => $request->remova_reward,
            'operational_money' => $request->operational_money,
            'housing_to_business' => $request->housing_to_business,
            'equity_replace_area' => $request->equity_replace_area,
            'receivable_money' => $request->receivable_money,
        ];

        $contarctInfo = ContractDetail::where('contract_id', $request->id)->first();
        if($contarctInfo){
            ContractDetail::where('contract_id', $request->id)->update($contract_detail);

        }else{
            ContractDetail::create($contract_detail);

        }

        return  back()->with('status','填写合同信息成功！');
    }


    // 合同明细
    public function detail(Request $request)
    {
        $projects = Project::all();
        $blocks = Survey::select('id', 'block_name')->get();
        $houses = Houses::all();


        $where = function ($query) use ($request) {
            if ($request->has('project_id') and $request->project_id != '') {
                $query->whereProjectId($request->project_id);
            }

            if ($request->has('block_id') and $request->block_id != '') {
                $query->whereBlockId($request->block_id);
            }

        };



        $where_house = function ($query) use ($request) {
            if ($request->has('levy_house_num') and $request->levy_house_num != '') {
                $query->where('levy_house_num' ,$request->levy_house_num);
            }


            if ($request->has('owner') and $request->owner != '') {
                $query->where('owner', $request->owner);
            }
        };


        $contract_details = Contract::with(['projects' => function ($query) {
            $query->where('status', 2);

        }])->with(['houses' => function($query) use($where_house){

            $query->where($where_house);

        }])->with(['details' => function($query){
            $query->where('contract_id', '!=', '');

        }])->where($where)->where('is_sign', 1)->paginate(10);



        return view('admin.contract.detail', compact('projects', 'blocks', 'houses', 'contract_details'));
    }

}
